---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[


]]--
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 参数
    local SINGLE_ITEM_FIX_POWER_COST = 1        --- 单个物品修复消耗的电量
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 素材
    local assets =
    {
        Asset("ANIM", "anim/loramia_building_multi_armed_creation.zip"),
    }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- anim_init
    local function anim_init(inst)
        for i = 1, 10, 1 do
            inst.AnimState:OverrideSymbol("slot_"..i,"loramia_building_multi_armed_creation","empty")
        end
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 单个显示、多个显示
    local function slot_display(inst,slot_index,temp_item)
        --------------------------------------------------------------------------------------------------------
        --- 限制数字
            slot_index = math.clamp(slot_index,1,10)
        --------------------------------------------------------------------------------------------------------
        ---- 清空
            if temp_item == nil then
                inst.AnimState:OverrideSymbol("slot_"..slot_index,"loramia_building_multi_armed_creation","empty")
                return
            end
        --------------------------------------------------------------------------------------------------------
        ---- 配置图标，尝试兼容MOD
            local tar_atlas = nil
            local tar_image = nil
            local imagename = temp_item.nameoverride or temp_item.components.inventoryitem.imagename or temp_item.prefab
            imagename  = string.gsub(imagename,".tex", "") .. ".tex"
            local atlasname = temp_item.components.inventoryitem.atlasname or GetInventoryItemAtlas(imagename)
            if TheSim:AtlasContains(atlasname, imagename) then
                --- 官方物品
                tar_atlas = atlasname
                tar_image = imagename
                -- break
            else
                --- 自定义MOD物品
                atlasname = GetInventoryItemAtlas(imagename)
                atlasname = resolvefilepath_soft(atlasname) --为了兼容mod物品，不然是没有这道工序的
                tar_atlas = atlasname
                tar_image = imagename
                -- break
            end
            if tar_atlas and tar_image and TheSim:AtlasContains(tar_atlas, tar_image) then
                -- break
            else
                tar_atlas = nil
                tar_image = nil
            end

            if tar_image and tar_atlas then
                inst.AnimState:OverrideSymbol("slot_"..slot_index,tar_atlas,tar_image)
            else
                inst.AnimState:OverrideSymbol("slot_"..slot_index,"loramia_building_multi_armed_creation","empty")
            end
        --------------------------------------------------------------------------------------------------------
    end
    local function all_items_display(inst)
        for i = 1, 10, 1 do
            local item = inst.components.container.slots[i]
            slot_display(inst,i,item)
        end
    end
    local function container_close_event_install(inst)
        inst:DoTaskInTime(0,all_items_display)
        inst:ListenForEvent("onclose",all_items_display)
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---- 安装容器界面
    local function container_Widget_change(theContainer)
        -----------------------------------------------------------------------------------
        ----- 容器界面名 --- 要独特一点，避免冲突
        local container_widget_name = "loramia_building_multi_armed_creation"

        -----------------------------------------------------------------------------------
        ----- 检查和注册新的容器界面
        local all_container_widgets = require("containers")
        local params = all_container_widgets.params
        if params[container_widget_name] == nil then
            params[container_widget_name] = {
                widget =
                {
                    slotpos = {},
                    animbank = "ui_krampusbag_2x8",
                    animbuild = "ui_krampusbag_2x8",
                    --pos = Vector3(-5, -120, 0),
                    pos = Vector3(0, 0, 0),
                },
                type = "chest",
                acceptsstacks = true,
            }
            local start_x = -162
            -- local start_y = 240
            local start_y = 320
            local x_offset = 75
            local y_offset = 100
            for y = 1, 5 do
                table.insert(params[container_widget_name].widget.slotpos, Vector3(start_x, -y_offset * y + start_y, 0))
            end
            for y = 1, 5 do
                table.insert(params[container_widget_name].widget.slotpos, Vector3(start_x + x_offset, -y_offset * y + start_y, 0))
            end
            ------------------------------------------------------------------------------------------
            ---- item test
                params[container_widget_name].itemtestfn =  function(container_com, item, slot)
                    return true
                end
            ------------------------------------------------------------------------------------------

            ------------------------------------------------------------------------------------------
        end
        
        theContainer:WidgetSetup(container_widget_name)
        ------------------------------------------------------------------------
        --- 开关声音
            if theContainer.widget then
                theContainer.widget.closesound = "dontstarve_DLC001/common/shocked"
                theContainer.widget.opensound = "dontstarve_DLC001/common/shocked"
            end
        ------------------------------------------------------------------------
    end
    local function add_container_before_not_ismastersim_return(inst)
        -------------------------------------------------------------------------------------------------
        ------ 添加背包container组件    --- 必须在 SetPristine 之后，
        -- local container_WidgetSetup = "wobysmall"
        if TheWorld.ismastersim then
            inst:AddComponent("container")
            inst.components.container.openlimit = 1  ---- 限制1个人打开
            -- inst.components.container:WidgetSetup(container_WidgetSetup)
            container_Widget_change(inst.components.container)

        else
            inst.OnEntityReplicated = function(inst)
                container_Widget_change(inst.replica.container)
            end
        end
        -------------------------------------------------------------------------------------------------
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- item fix and power cost
    local com_checkers = {
        ["finiteuses"] = {
            test = function(inst,item)
                if item.components.finiteuses:GetPercent() >= 1 then
                    return
                end
                return true
            end,
            fix = function(inst,item)
                local max_finiteuses = item.components.finiteuses.total
                local fix_num = max_finiteuses * 1/100
                item.components.finiteuses:Use(-fix_num)
                if item.components.finiteuses:GetPercent() >= 1 then
                    item.components.finiteuses:SetPercent(1)
                end
            end,
        },
        ["armor"] = {
            test = function(inst,item)
                if item.components.armor:IsIndestructible() then
                    return
                end
                local maxcondition = item.components.armor.maxcondition
                local condition = item.components.armor.condition
                if condition >= maxcondition then
                    return
                end
                return true
            end,
            fix = function(inst,item)
                local maxcondition = item.components.armor.maxcondition
                local condition = item.components.armor.condition
                local fix_num = maxcondition * 1/100
                item.components.armor:SetCondition(condition + fix_num)
            end,
        }
    }
    local function single_item_fix(inst,item)
        ---------------------------------------------------------------
        --- 耐久度检查。同时处理 护甲耐久度 和 普通耐久度
            local check_ret = false
            for com, fns in pairs(com_checkers) do
                if item.components[com] ~= nil then
                    check_ret = fns.test(inst,item)
                end
            end
            if not check_ret then
                return
            end
        ---------------------------------------------------------------
        --- 电量不够
            if not inst.components.loramia_com_power_device:CanTakePower(SINGLE_ITEM_FIX_POWER_COST) then
                return
            end
        ---------------------------------------------------------------
        --- 修理
            if inst.components.loramia_com_power_device:IsGotPowerSucceed(SINGLE_ITEM_FIX_POWER_COST) then
                for com,fns in pairs(com_checkers) do
                    if item.components[com] ~= nil then
                        fns.fix(inst,item)
                    end
                end
            end
        ---------------------------------------------------------------

    end
    local function item_fix_task(inst)
        for i = 1, 10, 1 do
            local item = inst.components.container.slots[i]
            if item and item:IsValid() then
                single_item_fix(inst,item)
            end
        end
    end
    local function power_cost_modules_install(inst)
        inst:AddComponent("loramia_com_power_device") -- 耗电模块
        -- inst:DoPeriodicTask(5,item_fix_task)
        TheWorld.components.loramia_com_special_timer_for_theworld:AddTimer(inst,item_fix_task,5)
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- hammer workable
    local function on_work_callback(inst, worker,workleft, numworks)
        if worker and worker:HasTag("player") then
            return
        end
        inst.components.workable:SetWorkLeft(5)
    end
    local function on_finish_callback(inst,worker)
        if inst.components.container then
            inst.components.container:Close()
            inst.components.container:DropEverything()
        end
        local fx = SpawnPrefab("collapse_big")
        fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
        fx:SetMaterial("wood")
        inst:Remove()
    end
    local function hammer_workable_install(inst)
        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(5)
        inst.components.workable:SetOnWorkCallback(on_work_callback)
        inst.components.workable:SetOnFinishCallback(on_finish_callback)
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- building
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        inst.entity:AddMiniMapEntity()
        inst.MiniMapEntity:SetIcon("loramia_building_multi_armed_creation.tex")
        inst.MiniMapEntity:SetPriority(1)

        MakeObstaclePhysics(inst, 1)

        inst.AnimState:SetBank("loramia_building_multi_armed_creation")
        inst.AnimState:SetBuild("loramia_building_multi_armed_creation")
        inst.AnimState:PlayAnimation("idle",true)

        inst:AddTag("structure")
        inst:AddTag("loramia_building_multi_armed_creation")

        inst.entity:SetPristine()
        -------------------------------------------------------------------------------------------------
        --- 
            anim_init(inst)
            add_container_before_not_ismastersim_return(inst)
        -------------------------------------------------------------------------------------------------
        if not TheWorld.ismastersim then
            return inst
        end
        -------------------------------------------------------------------------------------------------
        --- 
            container_close_event_install(inst)
        -------------------------------------------------------------------------------------------------
        --- 
            power_cost_modules_install(inst)
        -------------------------------------------------------------------------------------------------
        --- 基础检查组件
            inst:AddComponent("inspectable")
            MakeHauntableLaunch(inst)
        -------------------------------------------------------------------------------------------------
        --- 可拆除
            hammer_workable_install(inst)
        -------------------------------------------------------------------------------------------------
        return inst
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---- 
    local function placer_postinit_fn(inst)
        anim_init(inst)
        TheWorld:PushEvent("loramia_com_power_battery_indicator_active",inst)
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
return Prefab("loramia_building_multi_armed_creation", fn, assets),
    MakePlacer("loramia_building_multi_armed_creation_placer", "loramia_building_multi_armed_creation", "loramia_building_multi_armed_creation", "idle", nil, nil, nil, nil, nil, nil, placer_postinit_fn, nil, nil)

